<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con">
            <span>编辑企业证书</span><div class="move_title"></div><?php include "../public/sub.php";?></div>
    </div>
    <div class="main-container">

        <div class="block-generic">


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>所属单位</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=chanid&type=dept&dept=chan&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="chanid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=chanid&type=dept&dept=chan&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="chanid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书分类</label>
                        <div class="layui-input-block">
                            <div id="type"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">证书编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="number" id="number" placeholder="请选择" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">证书说明</label>
                        <div class="layui-input-block">
                            <textarea id="introduce" name="introduce" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书形式</label>
                        <div class="layui-input-block">
                            <select class="layui-select form" id="form" name="form" lay-filter="form"></select>
                            <p class="error" id="form_error" style="margin: 0;"></p>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>证书状态</label>
                        <div class="layui-input-block">
                            <select class="layui-select " name="state" id="state" lay-filter="state"></select>
                            <p class="error" id="state_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发证单位</label>
                        <div class="layui-input-block">
                            <input type="text" name="issue_unit" id="issue_unit" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发证日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="issue_dt" id="issue_dt" placeholder="请选择" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>!</text>有效期止</label>
                        <div class="layui-input-block">
                            <input type="text" placeholder="请选择" name="expiry_dt" id="expiry_dt" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>!</text>下次年检时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="inspect_dt" id="inspect_dt" placeholder="请选择" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">自编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="zinumber" id="zinumber" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6 layui-hide" id="outDiv">
                        <label class="layui-form-label">注销时间</label>
                        <div class="layui-input-block">
                            <input type="text" name="out_dt" id="out_dt" placeholder="请输入" clearable autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">保管人</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=custodianid&type=use','770px','490px');"></div>
                                <div class="tanxiannei" id="custodianid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=custodianid&type=use','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="custodianid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">存放地</label>
                        <div class="layui-input-block">
                            <div id="store_addr"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    $("#commentForm").validate({
        rules: {
            name: {required: true},
        },
        messages:{
            name: {required: "请输入"},
        }
    });

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();



        facade.ajax({type: 'get', url: ask_cert_company_edit, data: {id: param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $('#name').val(data.name);
                $('#number').val(data.number);
                $('#zinumber').val(data.zinumber);
                $('#introduce').val(data.introduce);
                $('#issue_unit').val(data.issue_unit);
                $('#issue_dt').val(data.issue_dt);
                $('#inspect_dt').val(data.inspect_dt);
                $('#out_dt').val(data.out_dt);
                $('#explain').val(data.explain);
                $('#expiry_dt').val(data.expiry_dt);


                if(data.state == 2){
                    $('#outDiv').removeClass('layui-hide');
                }

                if(data.chanid_info){
                    tan_tags_init('#chanid', data.chanid_info);
                }
                if(data.custodian_info){
                    tan_tags_init('#custodianid', data.custodian_info);
                }



                var form = res.data.form;
                var form_str = '<option value=""></option>';
                for (var i in form){
                    if(data.form == i){
                        form_str += '<option value="'+i+'" selected>'+form[i]+'</option>';
                    }else{
                        form_str += '<option value="'+i+'">'+form[i]+'</option>';
                    }
                }
                $('#form').html(form_str);

                var state = res.data.state;
                var state_str = '';
                for (var i in state){
                    if(data.state == i){
                        state_str += '<option value="'+i+'" selected>'+state[i]+'</option>';
                    }else{
                        state_str += '<option value="'+i+'">'+state[i]+'</option>';
                    }
                }
                $('#state').html(state_str);

                xmselect_dan('type', res.data.type, data.type, function (res) {
                    if(res.arr && res.arr.length > 0){
                        $("#type").find('xm-select').removeClass('error');
                    }else{
                        $("#type").find('xm-select').addClass('error');
                    }
                });
                xmselect_dan('store_addr', res.data.store_addr, data.store_addr);



                public_upload_file_load('upload_files',res.data.files);
                render_from();
                render_date();
            }
        });

        form.on('select(state)', function(data){
            if(data.value == ''){
                $('#state_error').show().text('请选择');
                $('#state_error').parent().find('input').addClass('select_error');
            }else{
                $('#state_error').hide();
                $('#state_error').parent().find('input').removeClass('select_error');
            }
            if(data.value == 2 || data.value == 3){
                $('#outDiv').removeClass('layui-hide');
            }else{
                $('#outDiv').addClass('layui-hide');
            }
        });
        form.on('select(form)', function(data){
            if(data.value == ''){
                $('#form_error').show().text('请选择');
                $('#form_error').parent().find('input').addClass('select_error');
            }else{
                $('#form_error').hide();
                $('#form_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('submit(add)', function(data){
            var validRes = $("#commentForm").valid();
            data.field.custodianid = tan_tags_getvalue('#custodianid','user');
            data.field.chanid = tan_tags_getvalue("#chanid", 'dept');

            if(!data.field.chanid){
                $("#chanid").parents('.tanxian').addClass('tanerror');
                $('#chanid_tanerror').text('请选择');
                validRes = false;
            }else{
                $("#chanid").parents('.tanxian').removeClass('tanerror');
                $('#chanid_tanerror').text('');
            }
            if(data.field.form == ''){
                $('#form_error').show().text('请选择');
                $('#form_error').parent().find('input').addClass('select_error');
                validRes = false;
            }else{
                $('#form_error').hide();
                $('#form_error').parent().find('input').removeClass('select_error');
            }
            if(data.field.state == ''){
                $('#state_error').show().text('请选择');
                $('#state_error').parent().find('input').addClass('select_error');
                validRes = false;
            }else{
                $('#state_error').hide();
                $('#state_error').parent().find('input').removeClass('select_error');
            }

            if(!data.field.type){
                $("#type").find('xm-select').addClass('error');
                validRes = false;
            }else{
                $("#type").find('xm-select').removeClass('error');
            }
            if(!validRes){return false;}

            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            facade.ajax({url: ask_cert_company_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });

            return false;
        });

    });
</script>
</body>
</html>
